#include <iostream>
using std::cout;
using std::endl;

class Base{
public:
    Base()
    : _pbase(new int(10))
    { cout << "Base()" << endl; }

    virtual void display() const{
        cout << "*_pbase:" << *_pbase << endl;
    }
    
    virtual ~Base(){
        if(_pbase){
            delete _pbase;
            _pbase = nullptr;
        }
        cout << "~Base()" << endl;
    }

private:
    int * _pbase;
};

class Derived
: public Base
{
public:
    Derived()
    : Base()
    , _pderived(new int(20))
    {   cout << "Derived()" << endl;}

    virtual void display() const{
        cout << "*_pderived:" << *_pderived << endl;
    }

    ~Derived(){
        if(_pderived){
            delete _pderived;
            _pderived = nullptr;
        }
        cout << "~Derived()" << endl;
    }

private:
    int * _pderived;
};


void test0(){
    Base * p = new Derived();
    p->display();
    delete p;
}

int main(void){
    test0();
    return 0;
}
